<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Merchant extends Model
{
    public $table='merchant';

    public $timestamps = false;

    public $primaryKey = 'id_merchant';

    protected $guarded = [];

//    const default_merchant_id = 31;

    /*
     * 微信平台
     */
    public function wxplateForm()
    {
        return $this->hasOne(Wxplatform::class,'id_wxplatform','id_wxplatform')->select('appid','appsecret');
    }

    public static function findMerchantById($merchant_id)
    {
//        $merchant_id = (empty($merchant_id)?self::default_merchant_id:$merchant_id);
//        $merchant_id = $this->becrityId($merchant_id);
        return self::findOrFail($merchant_id);
    }

    public function encrityId($id)
    {
        return base64_encode(strrev(base64_encode($id)));
    }

    public function becrityId($id)
    {
        return base64_decode(strrev(base64_decode($id)));
    }

    public function memberOptionalFields()
    {
        return $this->belongsToMany(MemberOptionalField::class,'merchant_member_optionalfield','id_merchant','id_member_optionalfield');
    }
}
